package com.yz.xyt.service.impl;

import com.yz.xyt.constant.Constants;
import com.yz.xyt.domain.User;
import com.yz.xyt.mapper.UserMapper;
import com.yz.xyt.service.IUserService;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public User findUser(String userName){
        return userMapper.findByUserName(userName);
    }

    @Override
    public List<User> findList() {
        return userMapper.findList();
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    /**
     * 生成系统用户
     * @param userList
     * @return
     */
    @Transactional
    @Override
    public void createSystemAccount(List<User> userList) {
        //过滤重复账号，以工号或学号为准
        List<User> list = new ArrayList<>();

        userList.stream().forEach(user -> {
            User record = findUser(user.getUserName());
            if (record ==null){
                list.add(user);
            }
        });

        //加密用户密码，此时原始密码为明文
        list.stream().peek(user -> {
            user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        }).collect(Collectors.toList()).stream().forEach(this::insert);

    }

    @Transactional
    @Override
    public void updateByUserNameSelective(User record) {
        userMapper.updateByUserNameSelective(record);
    }

    @Transactional
    @Override
    public int deleteByUserName(String userName) {
        return userMapper.deleteByUserName(userName);
    }

    @Transactional
    @Override
    public int resetPassword(User user) {

        //查询系统记录的用户
        User record = userMapper.findByUserName(user.getUserName());
        //使用系统默认密码
        String newPasswordStr = Constants.DEFAULT_PASSWORD;
        //使用系统用户中手机号后6位重置密码
//        if (StringUtils.isNotEmpty(record.getPhone())){
//            String phone = record.getPhone();
//            newPasswordStr = phone.substring(phone.length() -6);
//        }
        //加密密码
        String newPassword = SecurityUtils.encryptPassword(newPasswordStr);
        record.setPassword(newPassword);
        return userMapper.updateByPrimaryKeySelective(record);
    }
}
